<?php
/* 用户表 */
namespace app\console\model;
use think\Db;
use think\Model;
use think\Log;

class SendJpush extends Model {
    private function jpushHttpPost($url, $data, $system_type = 1) {
        $header[] = "Content-type:application/json";
        
        if ($system_type == 1) {
            //安卓使用
            $header[] = "Authorization: Basic " . base64_encode("18b3e0866fb0af8e4c546faf:ffa5fdbf23857c63b6dbbb92");//正式服务器
            //$header[] = "Authorization: Basic ".base64_encode("cc8f76c81922b48291695d4b:7c58281d9e3da9c4ac0e998b");//测试服務器
        } elseif ($system_type == 2) {
            //IOS使用
            $header[] = "Authorization: Basic " . base64_encode("43ef4060f3d2287663990037:b4221f2f19e6fc9071930853");//正式服务器
        }
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $response = curl_exec($ch);
        
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
        Log::write("发送push：" . json_encode($response, JSON_UNESCAPED_UNICODE), Log::INFO);
        return $response;
    }
    
    private function jpush($msg, $systemType) {
        $data = $msg;
        $url  = 'https://api.jpush.cn/v3/push';
        if ($systemType) {
            $result = $this->jpushHttpPost($url, $data, $systemType);
        } else {
            $result=$this->jpushHttpPost($url, $data, 1);
            $result1=json_decode($result,true);
            if(isset($result1['sendno']) && $result1['sendno']=='0'){
                return $result;
            }
            $result=$this->jpushHttpPost($url, $data, 2);
            return $result;
        }
        
        return $result;
    }
    
    private function sendPush($data, $msgType, $push_id, $msgContent, $systemType) {
        $res = [
            'platform'     => 'all',
            'audience'     => [
                'registration_id' => [$push_id],
            ],
            'notification' =>[
                'alert'   => '',
                'android' => [
                    'alert'      => $msgContent,
                    'title'      => '天神贷客户端',
                    'builder_id' => 3,
                    'extras'     => [
                        'msg_type'    => $msgType,
                        'msg_content' => $data
                    ],
                ],
                'ios'     => [
                    'alert'  => $msgContent,
                    "badge"  => "1",
                    "sound"  => "default",
                    'extras' => [
                        'msg_type'    => $msgType,
                        'msg_content' => $data
                    ],
                ],
            ],
            'options'      => [
                "time_to_live"    => 24 * 60 * 60, //保存离线时间的秒数默认为一天
                'apns_production' => TRUE
            ],
        ];
        $res=json_encode($res);
        $result = $this->jpush($res, $systemType);
        Log::write('消息发送数据:'.$res.'<>发送结果:'.$result);
        return $result;
    }
    
    
    
    
    /***
     * 推送消息
     * @param $consumeId 订单id or 用户id(当$msgIdType为'order'时为订单id,当$msgIdType为'system'时为用户id)
     * @param $msgType 消息类型:(1:审核通过;2:审核失败;3:放款成功;4:已还款;5:系统消息数量;99:提示转掌众推送;100:订单更新)
     * @param $systemType 系统类型
     * **/
    public function pushUnreadMessages($data,$title=''){
        if(!$pushId=$data['pushId']){
            return false;
        }
        $msgContent            =$title ? $title : "您有新消息，请注意查看。";
        $postData['message_count'] = isset($data['messageCount']) ? ((int)$data['messageCount'] ? (int)$data['messageCount'] : 1) : 1 ;
        $systemType= isset($data['systemType']) ? ((int)$data['systemType'] ? (int)$data['systemType'] : 0) : 0 ;
        $res = $this->sendPush($postData,5, $pushId, $msgContent, $systemType);
        return $res;
    }
}